package cn.tedu.antconlonysever.contract.dao.persist.repository.impl;

import cn.tedu.antcolonycommons.pojo.vo.PageData;
import cn.tedu.antcolonycommons.util.PageInfoToPageDataConverter;
import cn.tedu.antconlonysever.contract.dao.persist.mapper.OrderMapper;
import cn.tedu.antconlonysever.contract.dao.persist.repository.IOrderRepository;
import cn.tedu.antconlonysever.contract.pojo.entity.Order;
import cn.tedu.antconlonysever.contract.pojo.vo.OrderListVO;
import cn.tedu.antconlonysever.contract.pojo.vo.OrderStandardVO;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


import java.util.Collection;
import java.util.List;
@Slf4j
@Repository
public class OrderRepositoryImpl implements IOrderRepository {
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public int insert(Order order) {

        return orderMapper.insert(order);
    }

    @Override
    public int insertBatch(List<Order> orderList) {

        return orderMapper.insertBatch(orderList);
    }

    @Override
    public int deleteById(Long id) {
        return orderMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(Collection<Long> idList) {

        return orderMapper.deleteBatchIds(idList);
    }

    @Override
    public int update(Order order) {

        return orderMapper.updateById(order);
    }

    @Override
    public int count() {
        return orderMapper.selectCount(null);
    }

    @Override
    public int countCusId(Long cusId) {
        QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("cus_id",cusId);
        return orderMapper.selectCount(queryWrapper);
    }

    @Override
    public int countCusIdNotId(Long id, Long cusId) {
        QueryWrapper<Order> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("cus_id",cusId)
                .ne("id",id);
        return orderMapper.selectCount(queryWrapper);
    }

    @Override
    public OrderStandardVO getStandardById(Long id) {

        return orderMapper.getStandardById(id);
    }

    @Override
    public PageData<OrderListVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询订单列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<OrderListVO> list = orderMapper.list();
        PageInfo<OrderListVO> pageInfo = new PageInfo<>(list);
        PageData<OrderListVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
